package com.survey.wxapp.controller;

import com.survey.common.annotation.Anonymous;
import com.survey.common.core.controller.BaseController;
import com.survey.common.core.domain.AjaxResult;
import com.survey.common.core.domain.entity.SysUser;
import com.survey.system.service.ISysUserService;
import com.survey.wxapp.service.impl.DisWxuserServiceImplWxapp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.UnknownHostException;

@Anonymous
@RestController
@RequestMapping("/wx/user")
public class WxUserController extends BaseController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private DisWxuserServiceImplWxapp disWxuserService;

    /**
     * 根据用户编号获取详细信息
     */
    @PostMapping("/select")
    public AjaxResult getInfo(@RequestBody SysUser user) throws UnknownHostException {
        Long userId = user.getUserId();
        System.out.println("userId:" + userId);
        AjaxResult ajax = AjaxResult.success();
        String wxOpenid = userService.selectUserById(userId).getWxOpenid();
        System.out.println("wxOpenid:" + wxOpenid);
        SysUser wxUser = userService.selectUserById(userId);
        ajax.put("userName", wxUser.getUserName());
        ajax.put("wxOpenid", wxOpenid);
        ajax.put("userNickName", wxUser.getNickName());
        ajax.put("userId", wxUser.getUserId());
        ajax.put("userAvatar", wxUser.getAvatar());
        ajax.put("userCreateTime", wxUser.getCreateTime());

        /*DisWxuser wxuser = disWxuserService.selectDisWxuserByWxOpenid(wxOpenid);
        if (wxuser != null) {
            ajax.put("wxuser", wxuser);
        }

        if (StringUtils.isNotNull(userId))
        {
            SysUser sysUser = userService.selectUserById(userId);
            InetAddress localHost = InetAddress.getLocalHost();
            String hostAddress = localHost.getHostAddress();
            String baseUrl ="http://"+hostAddress+":8089";
            String url = sysUser.getAvatar();

            if (url != null && !url.isEmpty()) {
                sysUser.setAvatar(baseUrl + url);
            }
            ajax.put(AjaxResult.DATA_TAG, sysUser);
        }*/
        return ajax;
    }
}
